package com.bzkj.basicinfo.service.basic.impl;

import com.bzkj.basicinfo.domain.basic.BasicPicType;
import com.bzkj.basicinfo.mapper.BasicPicTypeMapper;
import com.bzkj.basicinfo.service.basic.BasicPicTypeService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class BasicPicTypeServiceImpl implements BasicPicTypeService
{

    @Autowired
    private BasicPicTypeMapper basicPicTypeMapper;

    @Override
    public List<BasicPicType> findBasicPicTypeData(BasicPicType entity)
    {
        return basicPicTypeMapper.findBasicPicTypeData(entity);
    }

    @Override
    public AjaxResult updateBasicPicType(BasicPicType entity)
    {
        int i = basicPicTypeMapper.updateBasicPicType(entity);
        return i > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败，未找到指定数据");
    }

    @Override
    public AjaxResult basicPicTypeSave(BasicPicType entity)
    {

        BasicPicType one = basicPicTypeMapper.findBasicPicTypeByCode(entity.getPicTypeCode());
        if(! Objects.isNull(one))
            return AjaxResult.error("该类型编码已存在");
        String userName = SecurityUtils.getLoginUser().getUser().getUserName();
        entity.setCreator(userName);
        entity.setCreatetime(new Date());
        basicPicTypeMapper.basicPicTypeSave(entity);
        return AjaxResult.success("新增成功");
    }

    @Override
    public R findBasicPicTypeAll()
    {
        List<BasicPicType> list = basicPicTypeMapper.findBasicPicTypeAll();
        return R.ok(list);
    }

    @Override
    public AjaxResult deleteBasicPicType(String id) {
        int i = basicPicTypeMapper.deleteById(id);
        if(i > 0){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败");
        }
    }
}
